﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using GBH.Models;
using GBH.Models.Dao;
using GBHNawaData;
using OfficeOpenXml;
using System.Text;
using System.IO;

namespace GBH.Controllers
{
    public class BranchApiController : ApiController
    {
        // GET api/branch
        public HttpResponseMessage GetAllActiveBranch()
        {
            var branchProvider = new BranchDao();
            return this.Request.CreateResponse(HttpStatusCode.OK,
            new { AllBranch = branchProvider.GetAllActiveBranch() });
        }

        public HttpResponseMessage PostSearchMaterialByIds(string branchno)
        {
            var branchLocationProvider = new MaterialLocationDao();
            var branchProivder = new BranchDao();
            var matWHProvider = new GBHNawaData.Dao.MaterialDao();
            var cutWHProvider = new GBHNawaData.Dao.CustomerDao();
            

            try
            {
                var branchDetail = branchProivder.GetBranchByBranchId(branchno);
                var branchLocation = branchLocationProvider.GetMatLocationByBranchNO(branchDetail.mprovtrans);
                var customerDetail = cutWHProvider.GetCustomerByCusNo(branchDetail.mcustno);

                var matDetails = matWHProvider.GetAllMaterialByIds(branchLocation.Select(b => b.MatNo).ToArray());
                return this.Request.CreateResponse(HttpStatusCode.OK,
                new
                {
                    BranchDetails = branchDetail,
                    MaterialInBranch = matDetails,
                    CustomerDetails = customerDetail
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public HttpResponseMessage GetExportMatByBranch(string branchno)
        {
            var branchProivder = new BranchDao();
            var branchLocationProvider = new MaterialLocationDao();
            var matWHProvider = new GBHNawaData.Dao.MaterialDao();
            var branchDetail = branchProivder.GetBranchByBranchId(branchno);
            var branchLocation = branchLocationProvider.GetMatLocationByBranchNO(branchDetail.mprovtrans);
            var matDetails = matWHProvider.GetAllMaterialByIds(branchLocation.Select(b => b.MatNo).ToArray());
            using (ExcelPackage pck = new ExcelPackage())
            {
                //Create the worksheet
                ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Material");
                ws.Cells["A1"].LoadFromCollection(matDetails, true);
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
                response.Content = new StreamContent(new MemoryStream(pck.GetAsByteArray()));
                response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                response.Content.Headers.Add("content-disposition", "attachment;  filename=MaterialInBranch.xlsx");
                return response;
            }
        }

       

        public bool PostSaveBranch(Branch branch)
        {
            var branchprovider = new Models.Dao.BranchDao();

            branchprovider.SaveBranch(branch);
            return true;
        }

        public List<vmsa120_customer_general> PostCustomerByName(string name)
        {
            var cusWHProvider = new GBHNawaData.Dao.CustomerDao();

            return cusWHProvider.GetCustomerByName(name.Trim());
        }

        public List<Branch> PostBranchByNameAndId(string branchno, string branchname)
        {
            var branchprovider = new Models.Dao.BranchDao();

            return branchprovider.GetBranchByBranchIdOrName(branchno, branchname);
        }
    }
}
